<template>
  <div class="videos-container">
    <div class="page-header">
      <h1>视频管理</h1>
      <button class="add-button" @click="showAddVideoDialog = true">
        <i class="fa fa-plus"></i>
        上传视频
      </button>
    </div>
    
    <!-- 搜索和筛选 -->
    <div class="search-filter">
      <div class="search-box">
        <i class="fa fa-search"></i>
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索视频标题、描述"
          @input="handleSearch"
        />
      </div>
      <div class="filter-group">
        <select v-model="filterCategory" @change="handleFilter">
          <option value="">全部分类</option>
          <option v-for="category in categories" :key="category.id" :value="category.id">
            {{ category.name }}
          </option>
        </select>
        <select v-model="filterStatus" @change="handleFilter">
          <option value="">全部状态</option>
          <option value="active">活跃</option>
          <option value="blocked">封禁</option>
          <option value="deleted">已删除</option>
        </select>
      </div>
    </div>
    
    <!-- 视频列表 -->
    <div class="videos-grid">
      <div v-for="video in paginatedVideos" :key="video.id" class="video-card">
        <div class="video-thumbnail">
          <img :src="video.thumbnail" alt="视频缩略图" />
          <span class="video-duration">{{ video.duration }}</span>
          <div class="video-actions-overlay">
            <button class="action-btn edit-btn" @click="editVideo(video)">
              <i class="fa fa-edit"></i>
            </button>
            <button class="action-btn status-btn" @click="changeVideoStatus(video)">
              <i v-if="video.status === 'blocked'" class="fa fa-eye"></i>
              <i v-else class="fa fa-eye-slash"></i>
            </button>
            <button class="action-btn delete-btn" @click="deleteVideo(video)">
              <i class="fa fa-trash"></i>
            </button>
          </div>
        </div>
        <div class="video-info">
          <h3 class="video-title">{{ video.title }}</h3>
          <div class="video-meta">
            <span class="video-views"><i class="fa fa-eye"></i> {{ video.views }}</span>
            <span class="video-likes"><i class="fa fa-thumbs-up"></i> {{ video.likes }}</span>
            <span class="video-comments"><i class="fa fa-comment"></i> {{ video.comments }}</span>
          </div>
          <div class="video-details">
            <span class="video-category">{{ getCategoryName(video.category) }}</span>
            <span class="video-status" :class="video.status">
              {{ video.status === 'active' ? '活跃' : video.status === 'blocked' ? '封禁' : '已删除' }}
            </span>
          </div>
          <div class="video-upload-time">
            {{ formatDate(video.uploadTime) }}
          </div>
        </div>
      </div>
      
      <!-- 空状态 -->
      <div v-if="filteredVideos.length === 0" class="empty-state">
        <i class="fa fa-video"></i>
        <p>暂无视频数据</p>
      </div>
    </div>
    
    <!-- 分页 -->
    <div v-if="filteredVideos.length > 0" class="pagination">
      <button :disabled="currentPage === 1" @click="currentPage = 1">
        <i class="fa fa-angle-double-left"></i>
      </button>
      <button :disabled="currentPage === 1" @click="currentPage--">
        <i class="fa fa-angle-left"></i>
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页
      </span>
      <button :disabled="currentPage === totalPages" @click="currentPage++">
        <i class="fa fa-angle-right"></i>
      </button>
      <button :disabled="currentPage === totalPages" @click="currentPage = totalPages">
        <i class="fa fa-angle-double-right"></i>
      </button>
    </div>
    
    <!-- 添加/编辑视频对话框 -->
    <div v-if="showAddVideoDialog || showEditVideoDialog" class="dialog-overlay" @click="closeDialog">
      <div class="dialog" @click.stop>
        <div class="dialog-header">
          <h3>{{ showEditVideoDialog ? '编辑视频' : '上传视频' }}</h3>
          <button class="close-button" @click="closeDialog">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div class="dialog-content">
          <form @submit.prevent="submitVideoForm">
            <div class="form-group">
              <label>视频标题</label>
              <input v-model="videoForm.title" type="text" required />
            </div>
            <div class="form-group">
              <label>视频描述</label>
              <textarea v-model="videoForm.description" rows="4"></textarea>
            </div>
            <div class="form-group">
              <label>分类</label>
              <select v-model="videoForm.category" required>
                <option value="" disabled>请选择分类</option>
                <option v-for="category in categories" :key="category.id" :value="category.id">
                  {{ category.name }}
                </option>
              </select>
            </div>
            <div class="form-group">
              <label>视频状态</label>
              <select v-model="videoForm.status" required>
                <option value="active">活跃</option>
                <option value="blocked">封禁</option>
                <option value="deleted">已删除</option>
              </select>
            </div>
            <div class="form-group">
              <label>是否推荐</label>
              <input v-model="videoForm.isRecommended" type="checkbox" />
            </div>
            <div class="form-group">
              <label>是否热门</label>
              <input v-model="videoForm.isHot" type="checkbox" />
            </div>
            <div class="dialog-footer">
              <button type="button" class="cancel-button" @click="closeDialog">取消</button>
              <button type="submit" class="save-button">{{ showEditVideoDialog ? '更新' : '上传' }}</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VideosView',
  data() {
    return {
      // 模拟分类数据
      categories: [
        { id: 'anime', name: '动画' },
        { id: 'music', name: '音乐' },
        { id: 'game', name: '游戏' },
        { id: 'entertainment', name: '娱乐' },
        { id: 'knowledge', name: '知识' },
        { id: 'tech', name: '科技' },
        { id: 'life', name: '生活' },
      ],
      // 模拟视频数据
      videos: [
        {
          id: 'video1',
          title: 'Vue3 入门教程',
          description: '详细介绍Vue3的核心概念和使用方法',
          thumbnail: '/src/assets/科技数码展宣传图.jpg',
          videoUrl: '#',
          category: 'knowledge',
          authorId: 'user4',
          views: 15234,
          likes: 1253,
          dislikes: 45,
          comments: 234,
          duration: '15:30',
          status: 'active',
          isRecommended: true,
          isHot: true,
          uploadTime: '2024-01-01T10:00:00Z',
        },
        {
          id: 'video2',
          title: '美食制作：意式肉酱面',
          description: '教你制作正宗的意大利肉酱面',
          thumbnail: '/src/assets/意式肉酱面.jpg',
          videoUrl: '#',
          category: 'food',
          authorId: 'user2',
          views: 8923,
          likes: 987,
          dislikes: 23,
          comments: 124,
          duration: '20:45',
          status: 'active',
          isRecommended: false,
          isHot: true,
          uploadTime: '2024-01-02T14:30:00Z',
        },
        {
          id: 'video3',
          title: '黑悟空游戏实况',
          description: '最新游戏黑悟空的实况解说',
          thumbnail: '/src/assets/黑悟空.jpg',
          videoUrl: '#',
          category: 'game',
          authorId: 'user3',
          views: 25678,
          likes: 3456,
          dislikes: 123,
          comments: 456,
          duration: '45:20',
          status: 'active',
          isRecommended: true,
          isHot: true,
          uploadTime: '2024-01-03T09:15:00Z',
        },
        {
          id: 'video4',
          title: '鬼灭之刃最新话分析',
          description: '鬼灭之刃第15集深度解析',
          thumbnail: '/src/assets/鬼灭之刃.jpeg',
          videoUrl: '#',
          category: 'anime',
          authorId: 'user1',
          views: 32456,
          likes: 5678,
          dislikes: 89,
          comments: 789,
          duration: '18:40',
          status: 'active',
          isRecommended: true,
          isHot: false,
          uploadTime: '2024-01-04T16:45:00Z',
        },
      ],
      searchQuery: '',
      filterCategory: '',
      filterStatus: '',
      currentPage: 1,
      itemsPerPage: 8,
      showAddVideoDialog: false,
      showEditVideoDialog: false,
      videoForm: {
        id: '',
        title: '',
        description: '',
        category: '',
        status: 'active',
        isRecommended: false,
        isHot: false,
      },
    }
  },
  computed: {
    filteredVideos() {
      let filtered = this.videos
      
      // 搜索
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase()
        filtered = filtered.filter(video => 
          video.title.toLowerCase().includes(query) || 
          video.description.toLowerCase().includes(query),
        )
      }
      
      // 分类筛选
      if (this.filterCategory) {
        filtered = filtered.filter(video => video.category === this.filterCategory)
      }
      
      // 状态筛选
      if (this.filterStatus) {
        filtered = filtered.filter(video => video.status === this.filterStatus)
      }
      
      return filtered
    },
    paginatedVideos() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.filteredVideos.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.filteredVideos.length / this.itemsPerPage)
    },
  },
  methods: {
    handleSearch() {
      this.currentPage = 1
    },
    handleFilter() {
      this.currentPage = 1
    },
    formatDate(dateString) {
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN')
    },
    getCategoryName(categoryId) {
      const category = this.categories.find(c => c.id === categoryId)
      return category ? category.name : '未知分类'
    },
    editVideo(video) {
      this.videoForm = { ...video }
      this.showEditVideoDialog = true
    },
    changeVideoStatus(video) {
      const newStatus = video.status === 'active' ? 'blocked' : 'active'
      if (confirm(`确定要将视频 "${video.title}" 的状态更改为${newStatus === 'active' ? '活跃' : '封禁'}吗？`)) {
        // 在实际应用中，这里应该调用API
        video.status = newStatus
      }
    },
    deleteVideo(video) {
      if (confirm(`确定要删除视频 "${video.title}" 吗？此操作不可撤销！`)) {
        // 在实际应用中，这里应该调用API
        const index = this.videos.findIndex(v => v.id === video.id)
        if (index > -1) {
          this.videos.splice(index, 1)
        }
      }
    },
    closeDialog() {
      this.showAddVideoDialog = false
      this.showEditVideoDialog = false
      this.resetForm()
    },
    resetForm() {
      this.videoForm = {
        id: '',
        title: '',
        description: '',
        category: '',
        status: 'active',
        isRecommended: false,
        isHot: false,
      }
    },
    submitVideoForm() {
      if (this.showEditVideoDialog) {
        // 编辑视频
        const index = this.videos.findIndex(v => v.id === this.videoForm.id)
        if (index > -1) {
          this.videos[index] = { ...this.videos[index], ...this.videoForm }
        }
      } else {
        // 添加视频
        const newVideo = {
          ...this.videoForm,
          id: 'video' + (this.videos.length + 1),
          thumbnail: '/src/assets/logo.png',
          videoUrl: '#',
          authorId: 'user1',
          views: 0,
          likes: 0,
          dislikes: 0,
          comments: 0,
          duration: '00:00',
          uploadTime: new Date().toISOString(),
        }
        this.videos.push(newVideo)
      }
      
      this.closeDialog()
    },
  },
}
</script>

<style scoped>
.videos-container {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  color: #2c3e50;
  margin: 0;
  font-size: 24px;
}

.add-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
}

.add-button:hover {
  background: #2980b9;
}

.add-button i {
  margin-right: 5px;
}

/* 搜索和筛选 */
.search-filter {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 20px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  min-width: 300px;
}

.search-box i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
}

.search-box input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.filter-group {
  display: flex;
  gap: 10px;
}

.filter-group select {
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  background: white;
}

/* 视频网格 */
.videos-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.video-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  transition: transform 0.3s, box-shadow 0.3s;
}

.video-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.15);
}

.video-thumbnail {
  position: relative;
  overflow: hidden;
  height: 180px;
}

.video-thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.video-card:hover .video-thumbnail img {
  transform: scale(1.05);
}

.video-duration {
  position: absolute;
  bottom: 10px;
  right: 10px;
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.video-actions-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s;
  gap: 10px;
}

.video-card:hover .video-actions-overlay {
  opacity: 1;
}

.action-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background-color 0.3s;
}

.edit-btn {
  background: #3498db;
}

.edit-btn:hover {
  background: #2980b9;
}

.status-btn {
  background: #f39c12;
}

.status-btn:hover {
  background: #e67e22;
}

.delete-btn {
  background: #e74c3c;
}

.delete-btn:hover {
  background: #c0392b;
}

.video-info {
  padding: 15px;
}

.video-title {
  margin: 0 0 10px 0;
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

.video-meta {
  display: flex;
  gap: 15px;
  margin-bottom: 10px;
  font-size: 14px;
  color: #7f8c8d;
}

.video-meta span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.video-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.video-category {
  padding: 3px 10px;
  background: #ecf0f1;
  border-radius: 12px;
  font-size: 12px;
  color: #7f8c8d;
}

.video-status {
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.video-status.active {
  background: #2ecc71;
  color: white;
}

.video-status.blocked {
  background: #f39c12;
  color: white;
}

.video-status.deleted {
  background: #e74c3c;
  color: white;
}

.video-upload-time {
  font-size: 12px;
  color: #95a5a6;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: #95a5a6;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.pagination button {
  padding: 8px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
}

/* 对话框 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #95a5a6;
}

.dialog-content {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #34495e;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.form-group input[type="checkbox"] {
  width: auto;
  margin-right: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 30px;
}

.cancel-button,
.save-button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.cancel-button {
  background: #ecf0f1;
  color: #7f8c8d;
}

.cancel-button:hover {
  background: #bdc3c7;
}

.save-button {
  background: #3498db;
  color: white;
}

.save-button:hover {
  background: #2980b9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-filter {
    flex-direction: column;
  }
  
  .filter-group {
    width: 100%;
  }
  
  .filter-group select {
    flex: 1;
  }
  
  .videos-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
  
  .pagination {
    flex-wrap: wrap;
  }
}
</style>